package com.example.game2048.module;


import com.example.game2048.annotation.GridType;
import com.example.game2048.bean.Pile;
import com.example.game2048.bean.Position;

import java.util.List;

public class ConfigPosition {
    public static void config(List<Position> positions){
        for(int i=0;i<positions.size();i++){
            if(hasLeft(i))
                positions.get(i).setLeftPosition(positions.get(i-1));
            if(hasRight(i))
                positions.get(i).setRightPosition(positions.get(i+1));
            if(hasTop(i))
                positions.get(i).setTopPosition(positions.get(i-GridType.GRID_SCALE));
            if(hasBottom(i))
                positions.get(i).setBottomPosition(positions.get(i+GridType.GRID_SCALE));
        }
    }
    private static boolean hasLeft(int i){
        return i % GridType.GRID_SCALE != 0;
    }

    private static boolean hasRight(int i){
        return (i+1) % GridType.GRID_SCALE != 0 ;
    }

    private static boolean hasTop(int i){
        return i >= GridType.GRID_SCALE;
    }

    private static boolean hasBottom(int i){
        return i < GridType.GRID_SCALE * (GridType.GRID_SCALE - 1);
    }

    public static void setOriginPositionOfPile(Position position){
        position.getPile().setActualX(position.getX());
        position.getPile().setActualY(position.getY());
    }
}
